<?php
$db = new Database;
$auth = Auth::getAuth('current_user');
$user_id = $auth['id'];
$company_id = $auth['company_id'];
if(isset($_POST['action'])){
    if($_POST['action']=="requestToApprove"){
        $numrowsRequest = $db->query("SELECT * FROM tbrequest r LEFT JOIN tbrequest_process rp on r.id=rp.request_id LEFT JOIN tbform f on f.id=r.form_id where (rp.approver='".roleType($user_groupType)."_".$user_group."' or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."' or rp.approver='".$getPosition['position']."') and rp.status=0 and r.status=0","numrows");
        echo $numrowsRequest;
    }else if($_POST['action']=="pendingRequest"){
        $numrowsRequest = $db->query("SELECT * FROM tbrequest r LEFT JOIN tbrequest_process rp on r.id=rp.request_id LEFT JOIN tbform f on f.id=r.form_id where r.user_id = '".$auth['id']."' and r.status = 0 and rp.status=0","numrows");
        echo $numrowsRequest;
    }else if($_POST['action']=="regUser"){
        $numrowsRegRequest = $db->query("SELECT * from tbuser where status = 0 and company_id = '$company_id' and is_active = 1","numrows");
        echo $numrowsRegRequest;
    }else if($_POST['action']=="userList"){
        $numrowsUserList = $db->query("SELECT * from tbuser where status = 1 and company_id = '$company_id' and is_active = 1","numrows");
        echo $numrowsUserList;
    }else if($_POST['action']=="deletedAccounts"){
        $numrowsDeleteAcct = $db->query("SELECT * from tbuser where company_id = '$company_id' and is_active = 0","numrows");
        echo $numrowsDeleteAcct;
    }else if($_POST['action']=="checkOrgChart"){
        $numrowsOrgchart = $db->query("SELECT * from tborgchart where company_id = '$company_id' and status = 1 and is_active = 1","numrows");
        echo $numrowsOrgchart;
    }else if($_POST['action']=="checkPositionRow"){
        $roleToInsert = $_POST['roleToInsert'];
        $roleTypeToInsert = $_POST['roleTypeToInsert'];
        if($roleTypeToInsert==1 || $roleTypeToInsert==2){
            $numrowsUserHead = $db->query("SELECT * from tbuser where role='$roleToInsert' and role_type='$roleTypeToInsert' and company_id='$company_id' and status = 1","numrows");
            if($numrowsUserHead==1){
                echo "doNotAccept";
            }
        }
    }else if($_POST['action']=="checkPositionRowEdit"){
        $userID = $_POST['userID'];
        $roleToInsert = $_POST['roleToInsert'];
        $roleTypeToInsert = $_POST['roleTypeToInsert'];
        if($roleTypeToInsert==1 || $roleTypeToInsert==2){
            $numrowsUserHead = $db->query("SELECT * from tbuser where role='$roleToInsert' and role_type='$roleTypeToInsert' and company_id='$company_id' and status = 1 and id!=$userID","numrows");
            if($numrowsUserHead==1){
                echo "doNotAccept";
            }
        }
    }else if($_POST['action']=="countOrgchart"){
        $numrowsDeleteAcct = $db->query("SELECT * from tborgchart where company_id = '$company_id'","numrows");
        echo $numrowsDeleteAcct;
    }
}
    
?>